package com.kdzy.lshy.socity_server.controller;

import com.kdzy.lshy.socity_server.service.WechartService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 微信小程序入口
 * @author cly
 * @since 2018-06-08
 */
@RestController
@RequestMapping("/wechart")
@CrossOrigin("*")
@Api(value = "WechartController", description = "微信小程序相关")
public class WechartController {

    private static final Logger logger = LoggerFactory.getLogger(WechartController.class);

    @Autowired
    private WechartService wechartService;

    /**
     * 微信小程序 - 验证是否注册（认证）
     * @param param 包含临时登录code，用来换取用户的openid和session_key、unionid 等信息
     * @return 结果集 - 是否需要弹出登录或者注册窗口
     */
    @RequestMapping(value = "/wechartLogin", method = RequestMethod.POST)
    @ApiOperation(notes = "/wechartLogin", httpMethod = "POST", value = "微信小程序 - 验证是否注册（认证）", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 5, message = "用户未注册（认证）；返回key_code_openid (换取openid)"),
            @ApiResponse(code = 6, message = "用户已注册（认证）"),
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 77, message = "数据库操作失败"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 12, message = "通过code向微信换取openid失败"),
            @ApiResponse(code = 13, message = "用户openid获取失败")
    })
    public Object wechartLogin(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"code\": \"临时登录code\",\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产\n" +
                            "}")
            String param, HttpServletRequest request){
        logger.info("进入《微信小程序 - 验证是否注册（认证）》wechartLogin()方法；入参param = " + param);
        return wechartService.wechartLogin(param, request);
    }

    /**
     * 网页端和小程序 - 登录
     * @param param 包含账户密码或者key_code_openid(小程序用)
     * @return 结果集 - 是否需要注册（认证）
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(notes = "/login", httpMethod = "POST", value = "网页端和小程序 - 登录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 5, message = "用户未注册（认证）"),
            @ApiResponse(code = 6, message = "用户已注册（认证）"),
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 77, message = "数据库操作失败"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常")
    })
    public Object login(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"dweller_idcard\": \"身份证号（网页端）,\"\n" +
                            " \"dweller_password\": \"密码\",\n" +
                            " \"key_code_openid\": \"(当visit_plat = 2时)换取openid用code\"\n" +
                            "}")
            String param, HttpServletRequest request){
        logger.info("进入《居民登录》login()方法；入参param = " + param);
        return wechartService.login(param, request);
    }

    /**
     * 微信小程序 - 注册（认证）（验证身份证和照片是否一致）
     * @param param 包含身份证图像和头像base64码信息
     * @return 结果集 是否认证成功
     */
    @RequestMapping(value = "/pageLogin", method = RequestMethod.POST)
    @ApiOperation(notes = "/pageLogin", httpMethod = "POST", value = "微信小程序 - 注册（认证）（验证身份证和照片是否一致）", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 0, message = "注册（认证）成功"),
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 881, message = "身份证图片信息为空"),
            @ApiResponse(code = 882, message = "头像图片信息为空"),
            @ApiResponse(code = 77, message = "数据库操作失败"),
            @ApiResponse(code = 771, message = "提取身份证信息失败"),
            @ApiResponse(code = 772, message = "上传身份证正面照"),
            @ApiResponse(code = 773, message = "身份证和头像比对失败"),
            @ApiResponse(code = 774, message = "账号和身份证图像信息不符"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常")
    })
    public Object pageLogin(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"idcardImg\": \"身份证图像base64:\",\n" +
                            " \"headImg\": \"本人头像图像base64:\",\n" +
                            " \"dweller_phone\": \"本人手机号\",\n" +
                            " \"dweller_idcard\": \"身份证号（网页端）\",\n" +
                            " \"dweller_password\": \"密码\",\n" +
                            " \"key_code_openid\": \"(当visit_plat = 2时)换取openid用code\"\n" +
                            "}")
            String param, HttpServletRequest request, HttpServletResponse response){
        logger.info("进入《微信小程序 - 注册（认证）》pageLogin()方法；入参param = " + param);
        return wechartService.pageLogin(param, request);
    }

    /**
     * 微信小程序 - 异步验证账号是否已注册
     * @param param 包含账号信息
     * @return 结果集 账号是否注册
     */
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    @ApiOperation(notes = "/regist", httpMethod = "POST", value = "微信小程序 - 异步验证账号是否已注册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 11, message = "账号不存在"),
            @ApiResponse(code = 12, message = "账号已存在")
    })
    public Object regist(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"dweller_idcard\": \"身份证号\"\n" +
                            "}")
            String param, HttpServletRequest request, HttpServletResponse response){
        logger.info("进入《微信小程序 - 异步验证账号是否已注册》regist()方法；入参param = " + param);
        return wechartService.regist(param, request);
    }

    /**
     * 网页和微信小程序 - 业务预约
     * @param param 预约信息
     * @return 结果集 预约结果
     */
    @RequestMapping(value = "/appointment", method = RequestMethod.POST)
    @ApiOperation(notes = "/appointment", httpMethod = "POST", value = "网页和微信小程序 - 业务预约", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 77, message = "数据库操作失败"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 33, message = "systoken过期"),
            @ApiResponse(code = 331, message = "systoken不一致"),
            @ApiResponse(code = 22, message = "预约日期冲突"),
            @ApiResponse(code = 0, message = "预约成功，给前台index，预约排位")
    })
    public Object appointment(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"time\": \"预约时间\",\n" +
                            " \"dep_id\": \"部门ID\",\n" +
                            " \"bizz_id\": \"业务ID\",\n" +
                            " \"is_socity_bizz\": \"业务员类型 0 社区直通 1 第三方接口 2其他\",\n" +
                            " \"bizz_type\":  业务办理状态 1 预约2 本社区待审核 3 本社区通过（4失败）5第三方已提交 6第三方待审核 7第三方成功（8第三方失败） 9 其他,\n" +
                            " \"dweller_id\": \"(当visit_plat = 0 时)居民id\",\n" +
                            " \"systoken\": \"(当visit_plat = 0 时)全局token\",\n" +
                            " \"key_code_openid\": \"(当visit_plat = 2时)换取openid用code\"\n" +
                            "}")
            String param, HttpServletRequest request){
        logger.info("进入《网页和微信小程序 - 业务预约》appointment()方法；入参param = " + param);
        return wechartService.appointment(param, request);
    }

    /**
     * 网页端和微信小程序 - 获取居民注册（认证）信息
     * @param param 包含居民id等信息
     * @return 结果集
     */
    @RequestMapping(value = "/getDwellerInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "/getDwellerInfo", httpMethod = "POST", value = "网页端和微信小程序 - 获取居民注册（认证）信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 77, message = "数据库操作失败"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 33, message = "systoken过期"),
            @ApiResponse(code = 331, message = "systoken不一致"),
            @ApiResponse(code = 332, message = "微信没有关联居民信息"),
            @ApiResponse(code = 1, message = "结果集为空"),
            @ApiResponse(code = 0, message = "成功，返回结果集")
    })
    public Object getDwellerInfo(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"dweller_id\": \"(当visit_plat = 0 时)居民id\",\n" +
                            " \"systoken\": \"(当visit_plat = 0 时)全局token\",\n" +
                            " \"key_code_openid\": \"(当visit_plat = 2时)换取openid用code\"\n" +
                            "}")
            String param, HttpServletRequest request){
        logger.info("进入《网页端和微信小程序 - 获取居民注册（认证）信息》getDwellerInfo()方法；入参param = " + param);
        return wechartService.getDwellerInfo(param, request);
    }

    /**
     * 网页端 - 登出
     * @param param 包含居民id，systoken等信息
     * @return 结果集 - 成功或者失败
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ApiOperation(notes = "/logout", httpMethod = "POST", value = "网页端 - 登出", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 33, message = "systoken过期"),
            @ApiResponse(code = 331, message = "systoken不一致"),
            @ApiResponse(code = 1, message = "登出失败"),
            @ApiResponse(code = 0, message = "登出成功")
    })
    public Object logout(
            @ApiParam(
                    required = true,
                    name = "param",
                    value = "{\n" +
                            " \"stamp\": 时间戳,\n" +
                            " \"is_used\": 是否有效 0:有效 1:无效,\n" +
                            " \"version\": 版本 0:测试 1:生产,\n" +
                            " \"visit_plat\": 访问平台 0:网页 1:一体机 2:小程序 3:其他,\n" +
                            " \"visit_type\": 访问类型 0:测试 1:生产,\n" +
                            " \"systoken\": \"(当visit_plat = 0 时)全局token\"\n" +
                            "}")
            String param, HttpServletRequest request){
        logger.info("进入《网页端 - 登出》logout()方法；入参param = " + param);
        return wechartService.logout(param, request);
    }

    /**
     * 微信小程序 - 获取用户信息
     * @param param 包含加密之后的用户信息
     * @return 结果集 - 是否成功
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "/getUserInfo", httpMethod = "POST", value = "微信小程序 - 获取用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object getUserInfo(String param, HttpServletRequest request){
        logger.info("进入《微信小程序 - 获取用户信息》getUserInfo()方法；入参param = " + param);
        return wechartService.queryUserInfo(param, request);
    }

    /**
     * 微信小程序 - 发送模板消息
     * @param param 包含form_id，保单提交Id
     * @return 结果集
     */
    @RequestMapping(value = "/sendTemplateMessage", method = RequestMethod.GET)
    @ApiOperation(notes = "/sendTemplateMessage", httpMethod = "GET", value = "微信小程序 - 发送模板消息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object sendTemplateMessage(@RequestParam(name = "param") String param){
        logger.info("进入《微信小程序 - 发送模板消息》sendTemplateMessage()方法；入参param = " + param);
        return wechartService.sendTemplateMessage(param);
    }

    /**
     * 网页端和微信小程序 - 图片上传
     * @param param 包含文件名等信息
     * @return 结果集
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation(notes = "/upload", httpMethod = "POST", value = "微信小程序 - 图片上传", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 98, message = "参数类型错误"),
            @ApiResponse(code = 88, message = "参数为空"),
            @ApiResponse(code = 99, message = "后台处理异常"),
            @ApiResponse(code = 1, message = "上传失败"),
            @ApiResponse(code = 0, message = "上传成功")
    })
    public Object upload(@ApiParam(name = "param", value = "0 - 网页端") String param, HttpServletRequest request, HttpServletResponse response){
        logger.info("进入《网页端和微信小程序 - 文件上传》upload()方法；入参param = " + param);
        return wechartService.upload(param, request, response);
    }

    /**
     * 微信小程序 - 文件下载
     * @return 结果集
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    @ApiOperation(notes = "/download", httpMethod = "GET", value = "微信小程序 - 文件下载", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object download(HttpServletRequest request, HttpServletResponse response){
            logger.info("进入《微信小程序 - 文件下载》download()方法");
            return wechartService.download(request, response);
    }

}
